<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 5.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" integrity="sha256-xejo6yLi6vGtAjcMIsY8BHdKsLg7QynVlFMzdQgUuy8=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" integrity="sha256-Vzbj7sDDS/woiFS3uNKo8eIuni59rjyNGtXfstRzStA=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"gz1234.gitee.io","root":"/","images":"/images","scheme":"Gemini","darkmode":true,"version":"8.12.3","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"flat"},"bookmark":{"enable":true,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content=". 控制台在 Node.js 中，使用 console对象代表控制台(在操作系统中表现为一个操作系统指定的字符界面，比如 Window 中的命令提示窗口)。  console.log console.info console.error 重定向到文件 console.warn console.dir console.time console.timeEnd console.trace consol">
<meta property="og:type" content="article">
<meta property="og:title" content="nodeCore">
<meta property="og:url" content="https://gz1234.gitee.io/2022/07/09/%E5%89%8D%E7%AB%AF%E8%AE%B0%E5%BD%95%E6%95%B4%E7%90%86/nodeCore/index.html">
<meta property="og:site_name" content="郭泽">
<meta property="og:description" content=". 控制台在 Node.js 中，使用 console对象代表控制台(在操作系统中表现为一个操作系统指定的字符界面，比如 Window 中的命令提示窗口)。  console.log console.info console.error 重定向到文件 console.warn console.dir console.time console.timeEnd console.trace consol">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-07-08T18:21:52.000Z">
<meta property="article:modified_time" content="2023-03-22T01:11:36.571Z">
<meta property="article:author" content="郭泽">
<meta property="article:tag" content="nodeCore">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://gz1234.gitee.io/2022/07/09/%E5%89%8D%E7%AB%AF%E8%AE%B0%E5%BD%95%E6%95%B4%E7%90%86/nodeCore/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://gz1234.gitee.io/2022/07/09/%E5%89%8D%E7%AB%AF%E8%AE%B0%E5%BD%95%E6%95%B4%E7%90%86/nodeCore/","path":"2022/07/09/前端记录整理/nodeCore/","title":"nodeCore"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>nodeCore | 郭泽</title>
  





  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">郭泽</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8E%A7%E5%88%B6%E5%8F%B0"><span class="nav-number">1.</span> <span class="nav-text">. 控制台</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%A8%E5%B1%80%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="nav-number">2.</span> <span class="nav-text">. 全局作用域</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%87%BD%E6%95%B0"><span class="nav-number">3.</span> <span class="nav-text">. 函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#process"><span class="nav-number">4.</span> <span class="nav-text">process</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#EventEmitter"><span class="nav-number">5.</span> <span class="nav-text">EventEmitter</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#util"><span class="nav-number">6.</span> <span class="nav-text">util</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#node-%E6%96%AD%E7%82%B9%E8%B0%83%E8%AF%95"><span class="nav-number">7.</span> <span class="nav-text">. node 断点调试</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">郭泽</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">54</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">21</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">51</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gz1234.gitee.io/2022/07/09/%E5%89%8D%E7%AB%AF%E8%AE%B0%E5%BD%95%E6%95%B4%E7%90%86/nodeCore/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="郭泽">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="郭泽">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="nodeCore | 郭泽">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          nodeCore
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-07-09 02:21:52" itemprop="dateCreated datePublished" datetime="2022-07-09T02:21:52+08:00">2022-07-09</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-03-22 09:11:36" itemprop="dateModified" datetime="2023-03-22T09:11:36+08:00">2023-03-22</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%89%8D%E7%AB%AF%E6%95%B4%E7%90%86/" itemprop="url" rel="index"><span itemprop="name">前端整理</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="控制台"><a href="#控制台" class="headerlink" title=". 控制台"></a>. 控制台</h2><p>在 Node.js 中，使用 <code>console</code>对象代表控制台(在操作系统中表现为一个操作系统指定的字符界面，比如 Window 中的命令提示窗口)。</p>
<ul>
<li>console.log</li>
<li>console.info</li>
<li>console.error 重定向到文件</li>
<li>console.warn</li>
<li>console.dir</li>
<li>console.time</li>
<li>console.timeEnd</li>
<li>console.trace</li>
<li>console.assert</li>
</ul>
<h2 id="全局作用域"><a href="#全局作用域" class="headerlink" title=". 全局作用域"></a>. 全局作用域</h2><ul>
<li>全局作用域(global)可以定义一些不需要通过任何模块的加载即可使用的变量、函数或类</li>
<li>定义全局变量时变量会成为 global 的属性。</li>
<li>永远不要不使用 var 关键字定义变量，以免污染全局作用域</li>
<li>setTimeout clearTimeout</li>
<li>setInterval clearInterval</li>
<li>unref 和 ref</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> test = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;callback&quot;</span>);</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">let</span> timer = <span class="built_in">setInterval</span>(test, <span class="number">1000</span>);</span><br><span class="line">timer.unref();</span><br><span class="line"><span class="built_in">setTimeout</span>(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  timer.ref();</span><br><span class="line">&#125;, <span class="number">3000</span>);</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<h2 id="函数"><a href="#函数" class="headerlink" title=". 函数"></a>. 函数</h2><ul>
<li>require</li>
<li>模块加载过程</li>
<li>require.resolve</li>
<li>模板缓存(require.cache)</li>
<li>require.main</li>
<li>模块导出</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports, <span class="built_in">require</span>, <span class="built_in">module</span>, filename, dirname;</span><br></pre></td></tr></table></figure>

<h2 id="process"><a href="#process" class="headerlink" title="process"></a>process</h2><p>在 node.js 里，process 对象代表 node.js 应用程序，可以获取应用程序的用户，运行环境等各种信息</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">process.argv.forEach(<span class="function"><span class="keyword">function</span> (<span class="params">item</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(item);</span><br><span class="line">&#125;);</span><br><span class="line">process.on(<span class="string">&quot;exit&quot;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;clear&quot;</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">process.on(<span class="string">&quot;uncaughtException&quot;</span>, <span class="function"><span class="keyword">function</span> (<span class="params">err</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(err);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(process.memoryUsage());</span><br><span class="line"><span class="built_in">console</span>.log(process.cwd());</span><br><span class="line"><span class="built_in">console</span>.log(__dirname);</span><br><span class="line">process.chdir(<span class="string">&quot;..&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(process.cwd());</span><br><span class="line"><span class="built_in">console</span>.log(__dirname);</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">err</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&quot;&amp;#x62A5;&amp;#x9519;&amp;#x4E86;&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line">err();</span><br></pre></td></tr></table></figure>

<ul>
<li>process.nextTick()方法将 callback 添加到”next tick 队列”。 一旦当前事件轮询队列的任务全部完成，在 next tick 队列中的所有 callbacks 会被依次调用。</li>
<li>setImmediate 预定立即执行的 callback，它是在 I/O 事件的回调之后被触发</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">setImmediate(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;4&quot;</span>);</span><br><span class="line">&#125;);</span><br><span class="line">setImmediate(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;5&quot;</span>);</span><br><span class="line">&#125;);</span><br><span class="line">process.nextTick(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;1&quot;</span>);</span><br><span class="line">  process.nextTick(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;2&quot;</span>);</span><br><span class="line">    process.nextTick(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="built_in">console</span>.log(<span class="string">&quot;3&quot;</span>);</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;next&quot;</span>);</span><br></pre></td></tr></table></figure>

<h2 id="EventEmitter"><a href="#EventEmitter" class="headerlink" title="EventEmitter"></a>EventEmitter</h2><p>在 Node.js 的用于实现各种事件处理的 event 模块中，定义了 EventEmitter 类，所以可能触发事件的对象都是一个继承自 EventEmitter 类的子类实例对象。</p>
<table>
<thead>
<tr>
<th>方法名和参数</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td>addListener(event,listener)</td>
<td align="left">对指定事件绑定事件处理函数</td>
</tr>
<tr>
<td>on(event,listener)</td>
<td align="left">对指定事件绑定事件处理函数</td>
</tr>
<tr>
<td>once(event,listener)</td>
<td align="left">对指定事件指定只执行一次的事件处理函数</td>
</tr>
<tr>
<td>removeListener(event,listener)</td>
<td align="left">对指定事件解除事件处理函数</td>
</tr>
<tr>
<td>removeAllListeners([event])</td>
<td align="left">对指定事件解除所有的事件处理函数</td>
</tr>
<tr>
<td>setMaxListeners(n)</td>
<td align="left">指定事件处理函数的最大数量.n为整数值，代表最大的可指定事件处理函数的数量</td>
</tr>
<tr>
<td>listeners(event)</td>
<td align="left">获取指定事件的所有事件处理函数</td>
</tr>
<tr>
<td>emit(event,[arg1],[arg2],[…])</td>
<td align="left">手工触发指定事件</td>
</tr>
</tbody></table>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> EventEmitter = <span class="built_in">require</span>(<span class="string">&#x27;./events&#x27;</span>);</span><br><span class="line"><span class="keyword">let</span> util = <span class="built_in">require</span>(<span class="string">&#x27;util&#x27;</span>);</span><br><span class="line">util.inherits(Bell,EventEmitter);</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Bell</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  EventEmitter.call(<span class="built_in">this</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> bell = <span class="keyword">new</span> Bell();</span><br><span class="line">bell.on(<span class="string">&#x27;newListener&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params">type,listener</span>)</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">`对 <span class="subst">$&#123;type&#125;</span>  事件增加<span class="subst">$&#123;listener&#125;</span>`</span>);</span><br><span class="line">&#125;);</span><br><span class="line">bell.on(<span class="string">&#x27;removeListener&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params">type,listener</span>)</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">`对<span class="subst">$&#123;type&#125;</span> 事件删除<span class="subst">$&#123;listener&#125;</span>`</span>);</span><br><span class="line">&#125;);</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">teacherIn</span>(<span class="params">thing</span>)</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">`老师带<span class="subst">$&#123;thing&#125;</span>进教室`</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">studentIn</span>(<span class="params">thing</span>)</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">`学生带<span class="subst">$&#123;thing&#125;</span>进教室`</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">masterIn</span>(<span class="params">thing</span>)</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">`校长带<span class="subst">$&#123;thing&#125;</span>进教室`</span>);</span><br><span class="line">&#125;</span><br><span class="line">bell.on(<span class="string">&#x27;响&#x27;</span>,teacherIn);</span><br><span class="line">bell.on(<span class="string">&#x27;响&#x27;</span>,studentIn);</span><br><span class="line">bell.once(<span class="string">&#x27;响&#x27;</span>,masterIn);</span><br><span class="line">bell.emit(<span class="string">&#x27;响&#x27;</span>,<span class="string">&#x27;书&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;==============&#x27;</span>);</span><br><span class="line">bell.emit(<span class="string">&#x27;响&#x27;</span>,<span class="string">&#x27;书&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;==============&#x27;</span>);</span><br><span class="line">bell.removeAllListeners(<span class="string">&#x27;响&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;==============&#x27;</span>);</span><br><span class="line">bell.emit(<span class="string">&#x27;响&#x27;</span>,<span class="string">&#x27;书&#x27;</span>);</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">EventEmitter</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  <span class="built_in">this</span>.events = &#123;&#125;;<span class="comment">//会把所有的事件监听函数放在这个对象里保存</span></span><br><span class="line">  <span class="comment">//指定给一个事件类型增加的监听函数数量最多有多少个</span></span><br><span class="line">  <span class="built_in">this</span>._maxListeners = <span class="number">10</span>;</span><br><span class="line">&#125;</span><br><span class="line">EventEmitter.prototype.setMaxListeners = <span class="function"><span class="keyword">function</span>(<span class="params">maxListeners</span>)</span>&#123;</span><br><span class="line">  <span class="built_in">this</span>._maxListeners = maxListeners;</span><br><span class="line">&#125;</span><br><span class="line">EventEmitter.prototype.listeners = <span class="function"><span class="keyword">function</span>(<span class="params">event</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="built_in">this</span>.events[event];</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//给指定的事件绑定事件处理函数，1参数是事件类型 2参数是事件监听函数</span></span><br><span class="line">EventEmitter.prototype.on = EventEmitter.prototype.addListener = <span class="function"><span class="keyword">function</span>(<span class="params">type,listener</span>)</span>&#123;</span><br><span class="line">  <span class="function"><span class="title">if</span>(<span class="params"><span class="built_in">this</span>.events[type]</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.events[type].push(listener);</span><br><span class="line">    <span class="function"><span class="title">if</span>(<span class="params"><span class="built_in">this</span>._maxListeners!=<span class="number">0</span>&amp;&amp;<span class="built_in">this</span>.events[type].length&gt;<span class="built_in">this</span>._maxListeners</span>)</span>&#123;</span><br><span class="line">      <span class="built_in">console</span>.error(<span class="string">`MaxListenersExceededWarning: Possible EventEmitter memory leak detected. <span class="subst">$&#123;<span class="built_in">this</span>.events[type].length&#125;</span> <span class="subst">$&#123;type&#125;</span> listeners added. Use emitter.setMaxListeners() to increase limit`</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    <span class="comment">//如果以前没有添加到此事件的监听函数，则赋一个数组</span></span><br><span class="line">    <span class="built_in">this</span>.events[type] = [listener];</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">EventEmitter.prototype.once = <span class="function"><span class="keyword">function</span>(<span class="params">type,listener</span>)</span>&#123;</span><br><span class="line">  <span class="comment">//用完即焚</span></span><br><span class="line"> <span class="keyword">let</span>  wrapper = <span class="function">(<span class="params">...rest</span>)=&gt;</span>&#123;</span><br><span class="line">   listener.apply(<span class="built_in">this</span>);<span class="comment">//先让原始的监听函数执行</span></span><br><span class="line">   <span class="built_in">this</span>.removeListener(type,wrapper);</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="built_in">this</span>.on(type,wrapper);</span><br><span class="line">&#125;</span><br><span class="line">EventEmitter.prototype.removeListener = <span class="function"><span class="keyword">function</span>(<span class="params">type,listener</span>)</span>&#123;</span><br><span class="line">  <span class="function"><span class="title">if</span>(<span class="params"><span class="built_in">this</span>.events[type]</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.events[type] = <span class="built_in">this</span>.events[type].filter(<span class="function"><span class="params">l</span>=&gt;</span>l!=listener)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//移除某个事件的所有监听函数</span></span><br><span class="line">EventEmitter.prototype.removeAllListeners = <span class="function"><span class="keyword">function</span>(<span class="params">type</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">delete</span> <span class="built_in">this</span>.events[type];</span><br><span class="line">&#125;</span><br><span class="line">EventEmitter.prototype.emit = <span class="function"><span class="keyword">function</span>(<span class="params">type,...rest</span>)</span>&#123;</span><br><span class="line">  <span class="built_in">this</span>.events[type]&amp;&amp;<span class="built_in">this</span>.events[type].forEach(<span class="function"><span class="params">listener</span>=&gt;</span>listener.apply(<span class="built_in">this</span>,rest));</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">module</span>.exports = EventEmitter;</span><br></pre></td></tr></table></figure>

<h2 id="util"><a href="#util" class="headerlink" title="util"></a>util</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> util = <span class="built_in">require</span>(<span class="string">&#x27;util&#x27;</span>);</span><br><span class="line"><span class="comment">//util.inherit();</span></span><br><span class="line"><span class="built_in">console</span>.log(util.inspect(&#123;<span class="attr">name</span>:<span class="string">&#x27;zfpx&#x27;</span>&#125;));</span><br><span class="line"><span class="built_in">console</span>.log(util.isArray([]));</span><br><span class="line"><span class="built_in">console</span>.log(util.isRegExp(<span class="regexp">/\d/</span>));</span><br><span class="line"><span class="built_in">console</span>.log(util.isDate(<span class="keyword">new</span> <span class="built_in">Date</span>()));</span><br><span class="line"><span class="built_in">console</span>.log(util.isError(<span class="keyword">new</span> <span class="built_in">Error</span>));</span><br></pre></td></tr></table></figure>

<h2 id="node-断点调试"><a href="#node-断点调试" class="headerlink" title=". node 断点调试"></a>. node 断点调试</h2><p>V8 提供了一个强大的调试器，可以通过 TCP 协议从外部访问。Nodejs 提供了一个内建调试器来帮助开发者调试应用程序。想要开启调试器我们需要在代码中加入 debugger 标签，当 Nodejs 执行到 debugger 标签时会自动暂停（debugger 标签相当于在代码中开启一个断点）。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line"><span class="keyword">var</span> b = <span class="string">&#x27;b&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">debugger</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> all = a + <span class="string">&#x27; &#x27;</span> + b;</span><br><span class="line"><span class="built_in">console</span>.log(all);</span><br></pre></td></tr></table></figure>

<table>
<thead>
<tr>
<th>命令</th>
<th align="left">用途</th>
</tr>
</thead>
<tbody><tr>
<td>c</td>
<td align="left">继续执行到下一个断点处</td>
</tr>
<tr>
<td>next,n</td>
<td align="left">单步执行</td>
</tr>
<tr>
<td>step,s</td>
<td align="left">单步进入函数</td>
</tr>
<tr>
<td>out,o</td>
<td align="left">退出当前函数</td>
</tr>
<tr>
<td>setBreakpoint(10),sb(10)</td>
<td align="left">在第10行设置断点</td>
</tr>
<tr>
<td>repl</td>
<td align="left">打开求值环境，ctrl_c退回debug模式</td>
</tr>
<tr>
<td>watch(exp)</td>
<td align="left">把表达式添加监视列表</td>
</tr>
<tr>
<td>watchers</td>
<td align="left">显示所有表达式的值</td>
</tr>
</tbody></table>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/nodeCore/" rel="tag"># nodeCore</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/07/09/%E5%89%8D%E7%AB%AF%E8%AE%B0%E5%BD%95%E6%95%B4%E7%90%86/packageJson/" rel="prev" title="package.json">
                  <i class="fa fa-chevron-left"></i> package.json
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/07/09/%E5%89%8D%E7%AB%AF%E8%AE%B0%E5%BD%95%E6%95%B4%E7%90%86/mySql/" rel="next" title="sql">
                  sql <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">郭泽</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js" integrity="sha256-yt2kYMy0w8AbtF89WXb2P1rfjcP/HTHLT7097U8Y5b8=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script>

  
<script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.0/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>

  <script class="next-config" data-name="pdf" type="application/json">{"object_url":{"url":"https://cdnjs.cloudflare.com/ajax/libs/pdfobject/2.2.8/pdfobject.min.js","integrity":"sha256-tu9j5pBilBQrWSDePOOajCUdz6hWsid/lBNzK4KgEPM="},"url":"/lib/pdf/web/viewer.html"}</script>
  <script src="/js/third-party/tags/pdf.js"></script>

  <script class="next-config" data-name="mermaid" type="application/json">{"enable":true,"theme":{"light":"default","dark":"dark"},"js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mermaid/9.1.3/mermaid.min.js","integrity":"sha256-TIYL00Rhw/8WaoUhYTLX9SKIEFdXxg+yMWSLVUbhiLg="}}</script>
  <script src="/js/third-party/tags/mermaid.js"></script>

  <script src="/js/third-party/fancybox.js"></script>


  





</body>
</html>
